using System;
using System.ComponentModel.DataAnnotations;
using System.Globalization;

namespace ERP.ERPSystem.WarehouseManagement.Dto
{
    /// <summary>
    /// 节假日设置创建DTO
    /// </summary>
    public class HolidaysSetCreateDto
    {
        private string _holidaysTimeString;
        private DateTime? _holidaysTime;

        /// <summary>
        /// 节假日日期字符串（用于接收前端数据）
        /// </summary>
        [Required(ErrorMessage = "节假日日期不能为空")]
        public string HolidaysTime 
        { 
            get => _holidaysTimeString;
            set 
            {
                _holidaysTimeString = value;
                _holidaysTime = null; // 重置缓存的DateTime
            }
        }

        /// <summary>
        /// 获取解析后的日期
        /// </summary>
        public DateTime GetHolidaysTime()
        {
            if (_holidaysTime.HasValue)
                return _holidaysTime.Value;

            if (string.IsNullOrEmpty(_holidaysTimeString))
                throw new ArgumentException("节假日日期不能为空");

            try
            {
                // 解析日期并设置为中午12点，避免时区转换问题
                if (DateTime.TryParseExact(_holidaysTimeString.Trim(), "yyyy-MM-dd", CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime result))
                {
                    // 设置为中午12点，避免午夜时区转换问题
                    _holidaysTime = new DateTime(result.Year, result.Month, result.Day, 12, 0, 0);
                    return _holidaysTime.Value;
                }

                // 备用解析
                if (DateTime.TryParse(_holidaysTimeString.Trim(), out DateTime fallbackResult))
                {
                    _holidaysTime = new DateTime(fallbackResult.Year, fallbackResult.Month, fallbackResult.Day, 12, 0, 0);
                    return _holidaysTime.Value;
                }

                throw new ArgumentException($"无法解析日期格式: {_holidaysTimeString}");
            }
            catch (Exception ex)
            {
                throw new ArgumentException($"日期解析失败: {_holidaysTimeString}, 错误: {ex.Message}");
            }
        }

        /// <summary>
        /// 节假日类型
        /// </summary>
        [Required(ErrorMessage = "节假日类型不能为空")]
        [StringLength(30, ErrorMessage = "节假日类型长度不能超过30个字符")]
        public string HolidaysType { get; set; }
    }
} 